#!/usr/bin/env bash
#  vim:ts=4:sts=4:sw=4:et
#
#  Author: Hari Sekhon
#  Date: 2016-12-17 19:47:36 +0000 (Sat, 17 Dec 2016)
#
#  https://github.com/harisekhon/Dockerfiles
#
#  License: see accompanying Hari Sekhon LICENSE file
#
#  If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish
#
#  https://www.linkedin.com/in/harisekhon
#

set -euo pipefail
[ -n "${DEBUG:-}" ] && set -x

docker_branch_base="$(basename "$(dirname "${BASH_SOURCE[0]}")/..")"
docker_branch_base="${docker_branch_base%-dev}"
docker_branch_base="${docker_branch_base%-cluster}"
if [ "$docker_branch_base" = "kafka" ]; then
    latest_tag="2.11-0.9"
else
    latest_tag="$(grep ARG.*_VERSION= Dockerfile | sed -n '$s/.*=//p' | awk -F. '{print $1"."$2}')"
fi

if [ "$SOURCE_BRANCH" = "$docker_branch_base-$latest_tag" ]; then
    for tag in latest ${latest_tag%%.*}; do
        docker tag "$IMAGE_NAME" "$DOCKER_REPO:$tag"
        docker push "$DOCKER_REPO:$tag"
    done
fi
